﻿using BDPAutodetect.Models;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace BDPAutodetect.Client.ViewWindows.WindowDialog
{
    /// <summary>
    /// SettingSlaveEditorDialog.xaml 的交互逻辑
    /// </summary>
    [ObservableObject]
    public partial class SettingSlaveEditorDialog : Window, IDialog<MSlave>
    {
        /// <summary>
        /// 显示标题
        /// </summary>
        [ObservableProperty]
        protected string titleName = "编辑";
        /// <summary>
        /// 显示状态信息
        /// </summary>
        [ObservableProperty]
        protected string statusMsg = string.Empty;
        /// <summary>
        /// 当前编辑参数界限数据
        /// </summary>
        [ObservableProperty]
        protected MSlave editorData;
        /// <summary>
        /// 源数据
        /// </summary>
        protected MSlave? SourceData { get; set; }
        /// <summary>
        /// 返回结果信息
        /// </summary>
        public MSlave? DialogResultData { get; set; }

        /// <summary>
        /// 构造函数
        /// </summary>
        public SettingSlaveEditorDialog(string argTitleName, MSlave? argSlave)
        {
            InitializeComponent();
            this.TitleName = argTitleName;
            this.EditorData = argSlave != null ? argSlave.CloneDeep() : new MSlave();
            this.SourceData = argSlave;
            this.DataContext = this;
        }

        /// <summary>
        /// 确认选择事件
        /// </summary>
        [RelayCommand]
        protected void ExecuteConfirmOk()
        {
            if (string.IsNullOrWhiteSpace(this.EditorData.Name))
            {
                this.StatusMsg = "名称不能为空";
                return;
            }
            if (this.SourceData != null)
            {
                if (this.EditorData.Name.Equals(this.SourceData.Name, StringComparison.OrdinalIgnoreCase)
                    && this.EditorData.NameNick == this.SourceData.NameNick)
                {
                    this.StatusMsg = "未调整，可以直接点击取消";
                    return;
                }
            }
            this.DialogResult = true;
            this.DialogResultData = this.EditorData;
            this.DialogResultData.Name = this.DialogResultData.Name.Trim();
            if (!string.IsNullOrWhiteSpace(this.DialogResultData.NameNick))
            {
                this.DialogResultData.NameNick = this.DialogResultData.NameNick.Trim();
            }
            this.Close();
        }

        /// <summary>
        /// 取消事件
        /// </summary>
        [RelayCommand]
        protected void ExecuteConfirmCancel()
        {
            this.DialogResult = false;
            this.Close();
        }
    }
}
